home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / creatorchanger.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  3.9 KB  |  154 lines

  1. import java.awt.Frame;
  2. import java.io.File;
  3. import java.util.Vector;
  4.  
  5. import com.apple.mrj.MRJOSType;
  6. import com.apple.mrj.macos.toolbox.ApplicationEventProxy;
  7. import com.apple.mrj.macos.toolbox.ApplicationEventListener;
  8.  
  9. /**
  10.  * This is a simple example of some of the features of the MRJ Toolkit.
  11.  * The CreatorChanger class will recursively traverse a folder hierarchy
  12.  * dropped on it and change all files of type 'TEXT' to have a creator
  13.  * of 'SNJ2' (Symantec's Visual Cafe 2.0).
  14.  * @author Levi Brown
  15.  * @author Apple Worldwide Developer Technical Support
  16.  * @author Apple Computer Inc.
  17.  * @version 1.0 10/28/1998
  18.  */
  19. public class CreatorChanger extends Frame implements ApplicationEventListener
  20. {
  21.     //The creator to change the file to.
  22.     static final MRJOSType defaultCreator = new MRJOSType("SNJ2");
  23.     //The type of file to change creator for.
  24.     static final MRJOSType defaultType = new MRJOSType("TEXT");
  25.  
  26.     //---------- ApplicationEventListener Implementation ----------//
  27.     /**
  28.      * Handles displaying about information for the application.
  29.      * This routine gets called by the MRJToolkit when the
  30.      * application should display about info.
  31.      */
  32.     public void aboutApplication()
  33.     {
  34.         (new AboutCCDialog(this)).setVisible(true);
  35.     }
  36.  
  37.     /**
  38.      * This method gets called when the application is opened via double-click.
  39.      */
  40.     public void openApplication()
  41.     {
  42.     }
  43.  
  44.     /**
  45.      * Handles opening of files dropped on the application.
  46.      * This routine gets called by MRJ for each drop batch.
  47.      */
  48.     public void openDocuments(java.io.File[] files)
  49.     {
  50.         BatchJob batchJob = new BatchJob(this, batchAction, files);
  51.         jobs.addElement(batchJob);
  52.         batchJob.processBatch(defaultType, defaultCreator);
  53.     }
  54.  
  55.     /**
  56.      * Handles printing of files for this application.
  57.      * This routine gets called by MRJ for each printing batch.
  58.      */
  59.     public void printDocuments(java.io.File[] files)
  60.     {
  61.     }
  62.  
  63.     /**
  64.      * Handles quitting the application.
  65.      * This routine gets called by the MRJToolkit when the
  66.      * application should quit.
  67.      */
  68.     public void quitApplication()
  69.     {
  70.         //Exit with success.
  71.         System.exit(0);
  72.     }    
  73.     //---------- End ApplicationEventListener Implementation ----------//
  74.  
  75.  
  76.     public void handleOpenFile(String[] files, String type, String creator)
  77.     {
  78.         MRJOSType myType;
  79.         MRJOSType myCreator;
  80.         try
  81.         {
  82.             myType        = new MRJOSType(type);
  83.             myCreator    = new MRJOSType(creator);
  84.         }
  85.         catch (Exception exc)
  86.         {
  87.             System.err.println("handleOpenFile: bad type and/or creator.  Using defaults.");
  88.             myType = defaultType;
  89.             myCreator = defaultCreator;
  90.         }
  91.         
  92.         File[] rawFiles = new File[files.length];
  93.         
  94.         for (int i = 0; i < files.length; ++i)
  95.         {
  96.             rawFiles[i] = new File(files[i]);
  97.         }
  98.         BatchJob batchJob = new BatchJob(this, batchAction, rawFiles);
  99.         jobs.addElement(batchJob);
  100.         batchJob.processBatch(myType, myCreator);
  101.     }
  102.  
  103.     /**
  104.      * Constructs a default instance of this class.
  105.      * Registers the new instance with MRJ Toolkit
  106.      * as an open document listener and quit listener.
  107.      */
  108.     public CreatorChanger()
  109.     {        
  110.         //Register MRJ handlers
  111.         ApplicationEventProxy.installListener(this);
  112.         //Create a vector to keep track of the jobs
  113.         jobs = new Vector();
  114.         //Create a listener to respond to jobs finishing
  115.         batchAction = new BatchAction();
  116.         
  117.         //{{INIT_CONTROLS
  118.         setLayout(null);
  119.         setBounds(-5000, -5000, 10,10);
  120.         setTitle("Untitled");
  121.         //}}
  122.         //{{INIT_MENUS
  123.         //}}
  124.     }
  125.     //{{DECLARE_CONTROLS
  126.     //}}
  127.     //{{DECLARE_MENUS
  128.     //}}
  129.  
  130.     /**
  131.      * Entry point for our application.
  132.      * Creates a new instance of this class.
  133.      */
  134.     static public void main(String args[])
  135.     {
  136.         CreatorChanger creatorChanger = new CreatorChanger();
  137.         creatorChanger.setVisible(true);
  138.     }
  139.  
  140.     class BatchAction implements java.awt.event.ActionListener
  141.     {
  142.         public void actionPerformed(java.awt.event.ActionEvent event)
  143.         {
  144.             Object object = event.getSource();
  145.             jobs.removeElement(object);
  146.             if (jobs.size() == 0)
  147.                 quitApplication();
  148.         }
  149.     }
  150.  
  151.     protected Vector jobs;
  152.     protected BatchAction batchAction;
  153. }
  154.